package cn.kgc.service.impl;

import cn.kgc.constants.Constants;
import cn.kgc.dto.BrandDto;
import cn.kgc.dto.ClassifyDto;
import cn.kgc.dto.ModelDto;
import cn.kgc.mapper.BrandMapper;
import cn.kgc.mapper.ModelMapper;
import cn.kgc.pojo.Classify;
import cn.kgc.mapper.ClassifyMapper;
import cn.kgc.service.ClassifyService;
import cn.kgc.utils.reids.RedisUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author fsw
 * @since 2021-08-04
 */
@Slf4j
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {

    private final ClassifyMapper classifyMapper;
    private final BrandMapper brandMapper;
    private final ModelMapper modelMapper;
    private final RedisUtils redisUtils;

    public ClassifyServiceImpl(ClassifyMapper classifyMapper, BrandMapper brandMapper, ModelMapper modelMapper, RedisUtils redisUtils) {
        this.classifyMapper = classifyMapper;
        this.brandMapper = brandMapper;
        this.modelMapper = modelMapper;
        this.redisUtils = redisUtils;
    }

    @Override
    public List<ClassifyDto> findClassifyTree() {
        List<ClassifyDto> list = null;
        //1.第一次查询的时候，从redis中获取数据
        String value = redisUtils.getValue(Constants.Redis.KEY_HOME_TREE);
        if (StringUtils.isBlank(value)){
            //1.2没数据  从mysql中查询，存到redis中，最后返回
            //1.查询所有的商品类别
            list = classifyMapper.findClassifyTree();
            log.info("所有的商品类别:{}", list);
            //如果查询的数据不为空
            if (list != null){
                //2.根据商品类别的id查询这个类别下所有的商品类型（型号）
                for (ClassifyDto classifyDto : list) {
                    //获取分类id
                    Long cid = classifyDto.getId();
                    List<BrandDto> brandDtoList = brandMapper.findBrandDtoListByClassifyId(cid);
                    log.info("所有品牌的集合:{}", brandDtoList);
                    //3.根据类别id + 品牌id  查询品牌下所有的商品类型（型号）
                    if (brandDtoList != null){
                        for (BrandDto brandDto : brandDtoList) {
                            //获取品牌id
                            Long bid = brandDto.getId();
                            /*QueryWrapper<Model> wrapper = new QueryWrapper<>();
                            wrapper.eq("brandId" , bid)
                                    .eq("classifyId" , cid);
                            List<ModelDto> modelDtoList = modelMapper.findModelDtoListByBrandIdAndClassifyId(wrapper);*/
                            List<ModelDto> modelDtoList = modelMapper.findModelDtoListByBrandIdAndClassifyId(cid , bid);
                            log.info("商品类型（型号）:{}", modelDtoList);
                            //将所有的商品类型（型号）添加到brandList中
                            brandDto.setModelList(modelDtoList);
                        }
                    }
                    //将所有的品牌添加到对应的分类中
                    classifyDto.setBrandList(brandDtoList);
                }
            }
            //将list转成json
            String json = JSON.toJSONString(list);
            //将json存入redis
            redisUtils.putValue(Constants.Redis.KEY_HOME_TREE, json);
        }else{
            //1.1有数据  直接返回  第一个参数是redis中返回的数据，第二个参数是list的泛型
            list = JSON.parseArray(value, ClassifyDto.class);
        }
        log.info("最终的数据:{}",list);
        return list;
    }
}
